/**
 * uniapp相关api
 */
class UniApi {
	static api;
	static config
	//提示loading
	static showLoading(message) {
	  if (uni.showLoading) {
	    // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
	    uni.showLoading({
	      title: message || '加载中...',
	      mask: true
	    });
	  } else {
	    // 低版本采用Toast兼容处理并将时间设为20秒以免自动消失
	    uni.showToast({
	      title: message || '加载中...',
	      icon: 'loading',
	      mask: true,
	      duration: 20000
	    });
	  }
	}
	//隐藏loading
	static hideLoading() {
	  if (uni.hideLoading) {
	    // 基础库 1.1.0 微信6.5.6版本开始支持，低版本需做兼容处理
	    uni.hideLoading();
	  } else {
	    uni.hideToast();
	  }
	}
	//设置缓存
	static async setStorage(key,value){
		return await UniApi.api.promisic(uni.setStorage)({key: key,data: value})
	}
	//获取缓存
	static async getStorage(key){
		return await UniApi.api.promisic(uni.getStorage)({key: key})
	}
	//删除缓存
	static async removeStorage(key){
		return await UniApi.api.promisic(uni.removeStorage)({key: key})
	}
	//跳转tab
	static async switchTab(obj){
		// const app = getApp();
		// if (app.globalData.isClicked) {
		//       return;
		// } 
		// app.globalData.isClicked = true;
		await uni.switchTab({
			url: obj.url,
			success: typeof (obj.success) === 'function' ? obj.success : function () { },
			fail: typeof (obj.fail) === 'function' ? obj.fail : function () { },
			complete: function () {
				// setTimeout(function () {
				//   app.globalData.isClicked = false;
				// }, UniApi.config.switchTabTime);
				if (typeof (obj.complete) === 'function') {
				    obj.complete()
				}
			}
		})
	}
	//跳转页面
	static async navigateTo(obj){
		// const app = getApp();
		// if (app.globalData.isClicked) {
		//       return;
		// }
		// app.globalData.isClicked = true;
		await uni.navigateTo({
			url: obj.url,
			success: typeof (obj.success) === 'function' ? obj.success : function () { },
			fail: typeof (obj.fail) === 'function' ? obj.fail : function () { },
			complete: function () {
				// setTimeout(function () {
				//   app.globalData.isClicked = false;
				// }, UniApi.config.navigateToTime);
				if (typeof (obj.complete) === 'function') {
				    obj.complete()
				}
			}
		})
	}
	//页面返回
	static async navigateBack({delta=1,animationType='pop-out',animationDuration=300}){
		// const app = getApp();
		// if (app.globalData.isClicked) {
		//       return;
		// }
		// app.globalData.isClicked = true;
		await uni.navigateBack({
			delta:delta,
			animationType:animationType,
			animationDuration:animationDuration
		});
		// setTimeout(function () {
		//   app.globalData.isClicked = false;
		// }, UniApi.config.navigateBackTime);
	}
	//提示框
	static showToast(options){
		uni.showToast({
			title: options.title || '加载中...',
			icon:'none',
			duration: 2000
		});
	}
	//弹出框
	static async showModal(params){
		return await UniApi.api.promisic(uni.showModal)({
		    title: params.title || '提示',
		    content: params.content || '网络错误...',
			showCancel:params.showCancel===false?false:true,
			cancelText:params.cancelText || '取消'
		}) 
	}
	//设置红点
	static setTabBarBadge(param){
		if(param.text>0){
			uni.setTabBarBadge({
			  index: param.index || 3,
			  text: param.text>99?'99+':param.text.toString()
			})
		}else{
			uni.removeTabBarBadge({
				index: param.index || 3,
			})
		}
	}
	//分享
	static onShareAppMessage({res,title,path,imageUrl,content,desc,bgImgUrl,query,templateId}){ 
		if (res && res.from === 'button') {// 来自页面内分享按钮
		      console.log(res)
		}
		const data = {
		  title: title || UniApi.config.onShareTitle,
		  path: path || 'pages/index/index',
		  success: typeof (success) === 'function' ? success : function () { },
		  fail: typeof (fail) === 'function' ? fail : function () { },
		  complete: typeof (complete) === 'function' ? complete : function () {}
		}
		imageUrl&&(data.imageUrl = imageUrl)
		content&&(data.content = content)
		desc&&(data.desc = desc)
		bgImgUrl&&(data.bgImgUrl = bgImgUrl)
		query&&(data.query = query)
		templateId&&(data.templateId = templateId)
		return data
	}
	//订单发货通知
	static async deliveryRelease(){
		var tmplIds = []
		// #ifdef MP-WEIXIN
			tmplIds = UniApi.config.wxTmplIdsA
		// #endif
		
		// #ifdef MP-TOUTIAO
			tmplIds = UniApi.config.ttTmplIdsA
		// #endif
		return await UniApi.api.promisic(uni.requestSubscribeMessage)({tmplIds: tmplIds})
	}
	// 检测session_key
	static async checkSession(){
		return await UniApi.api.promisic(uni.checkSession)({})
	}
	//微信支付
	static async requestPayment(data){
		var dataV = {}
		// #ifdef MP-WEIXIN
			dataV = {
				provider: 'wxpay',
				timeStamp: data.timestamp,
				nonceStr: data.nonceStr,
				package: data.package,
				signType: data.signType,
				paySign: data.paySign
			}
			
		// #endif
		
		// #ifdef MP-TOUTIAO
			const payChannel ={
				default_pay_channel:'wx'
			}
			dataV = {
				orderInfo: data,
				service: 3, // 不拉起字节跳动小程序收银台
				_debug:1,
				payChannel: payChannel
			}
		// #endif
		return await UniApi.api.promisic(uni.requestPayment)(dataV)
		
	}
	// 打开设置
	static async openSetting(){
		await UniApi.api.promisic(uni.openSetting)({})
	}
	//小程序的登陆
	static async loginData(){
		var provider='';
		// #ifdef MP-WEIXIN
			provider = 'weixin'
		// #endif
		
		// #ifdef MP-TOUTIAO
			provider = 'toutiao'
		// #endif
		
		const loginRes = await UniApi.api.promisic(uni.login)({provider: provider})
		const infoRes = await UniApi.api.promisic(uni.getUserInfo)({provider: provider})
		if(infoRes.errMsg != "getUserInfo:ok"){
			// #ifdef MP-TOUTIAO
				await UniApi.openSetting()
			// #endif
			return {type:0}
		}
			
		// #ifdef MP-TOUTIAO
			return {
				type:2,
				anonymousCode:loginRes.anonymousCode,
				code:loginRes.code,
				avatarUrl:infoRes.userInfo.avatarUrl,
				country:infoRes.userInfo.country,
				gender:infoRes.userInfo.gender,
				language:infoRes.userInfo.gender,
				nickName:infoRes.userInfo.nickName,
				province:infoRes.userInfo.province
			}
		// #endif
		// #ifdef MP-WEIXIN
		return {
			type:1,
			code:loginRes.code,
			encryptData:infoRes.encryptedData,
			iv:infoRes.iv
		}
		// #endif
	}
	//微信小程序的登陆
	static async wxCode(){
		return await UniApi.api.promisic(uni.login)({provider: 'weixin'})
	}
	//打电话
	static makePhoneCall(phoneNumber){
		uni.makePhoneCall({
		    phoneNumber:phoneNumber.toString()
		});
	}
}
export default UniApi